/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.facade.common;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.pay.chinapay.ChinaPay;
import org.grayrabbit.pay.chinapay.common.PayConstant.CURRENCY_CODE;
import org.grayrabbit.pay.chinapay.common.PayConstant.PAY_TRADE_TYPE;
import org.grayrabbit.pay.chinapay.config.ChinaPayConfigUtil;
import org.grayrabbit.pay.chinapay.config.ChinaPayInfo;
import org.grayrabbit.pay.core.model.entity.transaction.Trade;
import org.grayrabbit.pay.trade.TradeChinaPayInfo;

/**
 * 
 * @author wy
 * @version $Id: TradeParam.java, v 0.1 2012-11-6 下午3:58:40 wy Exp $
 */
public class TradeParam {
    /**
     * 组装表单参数
     * 
     * @param tradeChinaPayInfo
     * @param trade
     * @param isPay
     * @param payUrl 基础交易路径
     * @return
     */
    public static Map<String, String> getParams(TradeChinaPayInfo tradeChinaPayInfo, Trade trade,
                                                boolean isPay, String payUrl) {
        Map<String, String> params = new HashMap<String, String>();
        if (isPay) {
            params.put(FormParams.SEND_TRADE_ORDID, tradeChinaPayInfo.getTradeId());
            params.put(FormParams.SEND_TRADE_CURYID, CURRENCY_CODE.RMB.getCode());
            params.put(FormParams.SEND_TRADE_TRANSAMT, tradeChinaPayInfo.getMoney().getChinaPay());
            params
                .put(FormParams.SEND_TRADE_MERID, ChinaPayConfigUtil.getChinaPayInfo().getMemId());
            params.put(FormParams.SEND_TRADE_TRANSTYPE, PAY_TRADE_TYPE.TRANS_TYPE_PAY.getCode());
            params.put(FormParams.SEND_TRADE_TRANSDATE, tradeChinaPayInfo.getTransDate());
            params.put(FormParams.SEND_TRADE_VERSION, ChinaPayInfo.PAY_VERSION);
            params.put(FormParams.SEND_TRADE_BGRETURL, payUrl
                                                       + ChinaPayInfo.TRADE_BACKGROUND_RESULT_URL);
            params.put(
                FormParams.SEND_TRADE_PRIV1,
                trade.getAppSystemCode() + "[" + trade.getOrderId() + "]"
                        + (StringUtils.isNotBlank(trade.getMemo()) ? (" " + trade.getMemo()) : ""));
            params.put(FormParams.SEND_TRADE_CHKVALUE, tradeChinaPayInfo.getCheckValue());
            //add 0908 正式支付改造 网关
            params.put(FormParams.SEND_TRADE_GATEID, ChinaPayInfo.TRADE_GATEID);
        } else {
            params.put(FormParams.SEND_REFUND_ORDID, tradeChinaPayInfo.getTradeId());
            params.put(FormParams.SEND_REFUND_TRANSAMT, tradeChinaPayInfo.getMoney().getChinaPay());
            params.put(FormParams.SEND_REFUND_MERID, ChinaPayConfigUtil.getChinaPayInfo()
                .getMemId());
            params
                .put(FormParams.SEND_REFUND_TRANSTYPE, PAY_TRADE_TYPE.TRANS_TYPE_REFUND.getCode());
            params.put(FormParams.SEND_REFUND_TRANSDATE,
                new SimpleDateFormat("yyyyMMdd").format(trade.getTradeDate()));
            params.put(FormParams.SEND_REFUND_VERSION, ChinaPayInfo.REFUND_VERSION);
            params.put(FormParams.SEND_REFUND_RETURNURL,
                payUrl + ChinaPayInfo.REFUND_BACKGROUND_RESULT_URL);
            //退款时必须传入唯一，注：中文验证签名会有问题
            params.put(FormParams.SEND_REFUND_PRIV1,
                trade.getAppSystemCode() + "[" + trade.getOrderId() + "]");

            StringBuilder sb = new StringBuilder();
            sb.append(ChinaPayConfigUtil.getChinaPayInfo().getMemId());
            sb.append(new SimpleDateFormat("yyyyMMdd").format(trade.getTradeDate()));
            sb.append(PAY_TRADE_TYPE.TRANS_TYPE_REFUND.getCode());
            sb.append(tradeChinaPayInfo.getTradeId());
            sb.append(tradeChinaPayInfo.getMoney().getChinaPay());
            sb.append(trade.getAppSystemCode() + "[" + trade.getOrderId() + "]"
                      + (StringUtils.isNotBlank(trade.getMemo()) ? (" " + trade.getMemo()) : ""));
            String checkvalue = ChinaPay.sign(sb.toString());
            params.put(FormParams.SEND_REFUND_CHKVALUE, checkvalue);
        }

        return params;
    }
}
